<?php
// 本类由系统自动生成，仅供测试用途
class SchoolAction extends CommonAction {
    public function index(){
    $teacher_name = getinfo($_SESSION['schooler_id'],'teacher','XM');
    $pic =    getinfo($_SESSION['schooler_id'],'teacher','pic');
    $this->assign('teacher_name',$teacher_name);
    $this->assign('pic',$pic);

    $this->display();
    }

    public function proc_man(){
        if (!empty($_REQUEST['time']))
        {
			$aa = strtotime($_REQUEST['time']).','.(strtotime($_REQUEST['time'])+86400);
            $map['FBSJ'] = array('BETWEEN',$aa);
        }

        if (!empty($_REQUEST['title']))
        {
            $map['title'] = array('like','%'.$_REQUEST['title'].'%');
        }
        $map['school_id'] = $_SESSION['school_id'];
        $mschool_proclamation =M('school_proclamation');
        $this->_listpage($mschool_proclamation, $map, $sortBy = '', $asc = false , 0 , $db = '',$listRows='15');
        $this->display();
    }
    public function proc_save(){
        $id =$this ->_insert('School_proclamation');
        if ($id)
        {
            add_log($_SESSION['schooler_id'],'添加 ID:'.$id.'学校公告',2);

             //$this->ajaxReturn($result,"操作成功!",1);
             $this ->success('操作成功',__URL__.'/proc_man');
        }
    }


//学生方法
    public function stu_man()
    {
        $map['school_id']=$_SESSION['school_id'];
        if (!empty($_REQUEST['name']))
        {
            $map['XM'] = array('like','%'.$_REQUEST['name'].'%');
        }
        if (!empty($_REQUEST['class_id']))
        {
            $map['class_id'] = $_REQUEST['class_id'];
        }

        if (!empty($_REQUEST['tel']))
        {
            $mapp['LXDH'] = array('like','%'.$_REQUEST['tel'].'%');
            $mparent =M('parent');
            $parentidlist = $mparent->field('id') ->where($mapp)->select();
            foreach ($parentidlist as $pl) {
                $idlist .=$pl['id'].',';
                # code...
            }
            $idlist .= '0';

            $map['parent_id'] = array('in',$idlist);
        }
        $mstudent= M('student');

        $this->_listpage($mstudent, $map, $sortBy = '', $asc = false , 0 , $db = '',$listRows='15');
        $this->display();
    }
    public function stu_edit()
    {
        $id =$this->_get('id');
        $mstudent =M('student');
        $studentinfo =$mstudent ->find($id);
        $this->assign('studentinfo',$studentinfo);

        $mppower = M('parent_power');
        $maps['student_id'] =$id;
        $pplist =$mppower->where($maps)->select();
        foreach ($pplist as $pl) {
             $idlist .= $pl['parent_id'].',';
            # code...
        }
        $idlist =rtrim($idlist,',');
        $map['id'] = array('in',$idlist);
        $mparent=M('parent');
        $parentlist =$mparent->where($map)->select();

  //       $parent = M('parent');
  //       $parentinfo = $parent->find($studentinfo['parent_id']);
		// $this->assign('parentinfo',$parentinfo);
        $this->assign('parentlist',$parentlist);
        $this->display();

    }

    public function stu_update()
    {
        $data = $this->_post();
        if (!$data['XM']  or !$data['XH'] or !$data['class_id'])
        {
            $this->error('信息填写不完整！');
        }

        //$muser =M('user');
        
		if($_POST['student_pic'] != null){
			$data['pic'] = substr($_POST['student_pic'],1);
		}
		$data['id'] = $_POST['studentid'];
//        $data['school_id'] =$_SESSION['school_id'];
        $mstudent =M('student');
        $sid = $mstudent ->save($data);

        $map['id'] = $data['student_id'];
        $mstudent -> where($map)->save($data);
<<<<<<< HEAD
        add_log($_SESSION['schooler_id'],'修改 ID:'.$map['id'].'学生',2);
=======

        $this->_updatecopyparent($_POST['studentid']);
        add_log($_SESSION['teacher_id'],'修改 ID:'.$map['id'].'学生',2);
>>>>>>> 4d10520e2d379bccf159d357fbc2e5519f23428b
        $this ->success('操作成功',__URL__.'/stu_man');
    }


    public function _updatecopyparent($sid)
    {
        $datacopy = $this->_post();
        //print_r($datacopy);
        // print_r($_FILES);
        // exit;


        $XM =$datacopy['copy_XM'];
        $muser =M('user');
        $mparent=M('parent');
        for ($i=0; $i < count($XM); $i++) {
            if (empty($datacopy['copy_XM'][$i]) or empty($datacopy['copy_SJHM'][$i]))
            {
                $this->error('信息填写不完整！');
            } 
        }
        for ($i=0; $i < count($XM) ; $i++) { 
            $info='';
            $copyparent ='';
            if($_FILES['copy_img']['name'][$i] != null){
                //echo 1;
                import('ORG.Net.UploadFile');
                $id=$_SESSION['parent_id'];
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 3145728 ;// 设置附件上传大小
                $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->savePath =  './uploads/';// 设置附件上传目录
                if(!$upload->upload()) {// 上传错误提示错误信息
                       $this->error($this->error($upload->getErrorMsg()));
                        //$this ->_error(308);
                }else{
                    $info = $upload->getUploadFileInfo();
                    $copyparent['pic'] = 'uploads/'.$info[0]['savename'];

                }
            }
            if (!empty($datacopy['copy_parent_id'][$i]))
            {
                $copyparent['XM'] = $datacopy['copy_XM'][$i];
                $copyparent['XBM'] = $datacopy['copy_XBM'][$i];
                $copyparent['DZXX'] = $datacopy['copy_DZXX'][$i];
                $copyparent['qq'] = $datacopy['copy_QQ'][$i];
                $copyparent['birthday'] = $datacopy['copy_birthday'][$i];
                $mparent->where('id = '.$datacopy['copy_parent_id'][$i])->save($copyparent);
               
            }
            else
            {
                $userinfo='';
                $userinfo =$muser->where("SJHM ='".$datacopy['copy_SJHM'][$i]."'")->find();
                if (empty($userinfo))
                {
                    $copyparent['XM'] = $datacopy['copy_XM'][$i];              
                    $copyparent['XBM'] = $datacopy['copy_XBM'][$i];
                    $copyparent['DZXX'] = $datacopy['copy_DZXX'][$i];
                    $copyparent['qq'] = $datacopy['copy_QQ'][$i];
                    $copyparent['birthday'] = $datacopy['copy_birthday'][$i];
                    $copyparent['user_id'] = $this->_create_user('parent','','',$datacopy['copy_SJHM'][$i]);
                    $copyparent['FBSJ'] = time();
                    $meparent_id = $mparent->add($copyparent);
                    $this->_addparentpower($sid,$meparent_id,$datacopy['copy_relation'][$i]);

                }
                else
                {
                    $parentinfo =loadinfo($userinfo['id'],2);
                    if (empty($parentinfo))
                    {
                        $copyparent['XM'] = $datacopy['copy_XM'][$i];
                        $copyparent['XBM'] = $datacopy['copy_XBM'][$i];
                        $copyparent['DZXX'] = $datacopy['copy_DZXX'][$i];
                        $copyparent['qq'] = $datacopy['copy_QQ'][$i];
                        $copyparent['birthday'] = $datacopy['copy_birthday'][$i];
                        $copyparent['user_id'] = $userinfo['id'];
                        $meparent_id =$mparent->add($copyparent);
                        $this->_addparentpower($sid,$meparent_id,$datacopy['copy_relation'][$i]);   
                    }
                    else
                    {
                        $copyparent['XM'] = $datacopy['copy_XM'][$i];
                        $copyparent['XBM'] = $data['copy_XBM'][$i];
                        $copyparent['DZXX'] = $data['copy_DZXX'][$i];
                        $copyparent['qq'] = $data['copy_QQ'][$i];
                        $copyparent['birthday'] = $data['copy_birthday'][$i];
                        $copyparent['user_id'] = $userinfo['id'];
                        $mparent->where('id = '.$parentinfo['id'])->save($copyparent);
                        
                        $this->_addparentpower($sid,$parentinfo['id'],$datacopy['copy_relation'][$i]);


                    }

                }
            }
        }

    }
    public function _addcopyparent($sid)
    {
        $datacopy = $this->_post();
        $XM =$datacopy['copy_XM'];
        $muser =M('user');
        $mparent=M('parent');
        //print_r($datacopy);
        //exit;
        for ($i=0; $i < count($XM); $i++) {
            if (empty($datacopy['copy_XM'][$i]) or empty($datacopy['copy_SJHM'][$i]))
            {
                $this->error('信息填写不完整！');
            } 
        }
        for ($i=0; $i < count($XM) ; $i++) { 
            # code...
            $info='';
            $copyparent ='';
            if($_FILES['copy_img']['name'][$i] != null){
                //echo 1;
                import('ORG.Net.UploadFile');
                $id=$_SESSION['parent_id'];
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 3145728 ;// 设置附件上传大小
                $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->savePath =  './uploads/';// 设置附件上传目录
                if(!$upload->upload()) {// 上传错误提示错误信息
                       $this->error($this->error($upload->getErrorMsg()));
                        //$this ->_error(308);
                }else{
                    $info = $upload->getUploadFileInfo();
                    $copyparent['pic'] = 'uploads/'.$info[0]['savename'];

                }
            }
            $userinfo='';
            $userinfo =$muser->where("SJHM ='".$datacopy['copy_SJHM'][$i]."'")->find();
            if (empty($userinfo))
            {
                $copyparent['XM'] = $datacopy['copy_XM'][$i];              
                $copyparent['XBM'] = $datacopy['copy_XBM'][$i];
                $copyparent['DZXX'] = $datacopy['copy_DZXX'][$i];
                $copyparent['qq'] = $datacopy['copy_QQ'][$i];
                $copyparent['birthday'] = $datacopy['copy_birthday'][$i];
                $copyparent['user_id'] = $this->_create_user('parent','','',$datacopy['copy_SJHM'][$i]);
                $copyparent['FBSJ'] = time();
                $meparent_id = $mparent->add($copyparent);

               
                $this->_addparentpower($sid,$meparent_id,$datacopy['copy_relation'][$i]);

            }
            else
            {
                $parentinfo =loadinfo($userinfo['id'],2);
                if (empty($parentinfo))
                {
                    $copyparent['XM'] = $datacopy['copy_XM'][$i];
                    $copyparent['XBM'] = $datacopy['copy_XBM'][$i];
                    $copyparent['DZXX'] = $datacopy['copy_DZXX'][$i];
                    $copyparent['qq'] = $datacopy['copy_QQ'][$i];
                    $copyparent['birthday'] = $datacopy['copy_birthday'][$i];
                    $copyparent['user_id'] = $userinfo['id'];
                    $meparent_id =$mparent->add($copyparent);
                    $this->_addparentpower($sid,$meparent_id,$datacopy['copy_relation'][$i]);   
                }
                else
                {
                    $copyparent['XM'] = $datacopy['copy_XM'][$i];
                    $copyparent['XBM'] = $data['copy_XBM'][$i];
                    $copyparent['DZXX'] = $data['copy_DZXX'][$i];
                    $copyparent['qq'] = $data['copy_QQ'][$i];
                    $copyparent['birthday'] = $data['copy_birthday'][$i];
                    $copyparent['user_id'] = $userinfo['id'];
                    $mparent->where('id = '.$parentinfo['id'])->save($copyparent);
                    
                    $this->_addparentpower($sid,$parentinfo['id'],$datacopy['copy_relation'][$i]);


                }

            }

        }

    }

    public function _addparentpower($sid,$parent_id,$relation)
    {
        $m=M('parent_power');
        $data['parent_id']=$parent_id;
        $data['student_id']=$sid;
        $data['relation']=$relation;
        $m->add($data);

    }
    public function stu_save()
    {
        $data = $this->_post();
        if (!$data['XM']  or !$data['XH'] or !$data['class_id'])
        {
            $this->error('信息填写不完整！');
        }
        
        $data['pic'] = substr($_POST['student_pic'],1);
        $data['school_id'] =$_SESSION['school_id'];
        $mstudent =M('student');
        $sid = $mstudent ->add($data);
<<<<<<< HEAD
        add_log($_SESSION['schooler_id'],'添加 ID:'.$sid.'学生',2);
=======
        $this->_addcopyparent($sid);
        add_log($_SESSION['teacher_id'],'添加 ID:'.$sid.'学生',2);
>>>>>>> 4d10520e2d379bccf159d357fbc2e5519f23428b

        $this ->success('操作成功',__URL__.'/stu_man');
    }


    public function stu_doimport(){
        $class_id = $this->_isempty($_POST['class_id'],'请选择班级');
        $dir = dirname(_FILE_);//获得编译文件当前目录
        $dir = str_replace("\\", "/", $dir)."/";
        $time = time();
        $filename = 'sutdent'.$time.'.xls';
        $path = $dir.$filename;
        $path = "./excel/".$filename;
        $result = move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path);
        if ($result){
            Vendor('excel_reader2');//调用第三方类，可以是require_once('指向reader.php文件所在的路径')
            $data = new Spreadsheet_Excel_Reader();
            $data->setOutputEncoding('utf-8');

            error_reporting(E_ALL ^ E_NOTICE);
            $data->read($path);

            $stu = M('student');
            $parent = M('parent');
            for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
                $arr = array();
                for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                    $arr[] = $data->sheets[0]['cells'][$i][$j];
                }
                mysql_query("set names 'utf-8'");

                if($arr[0] !="" || $arr[0] != null){
                    $map['XM'] = $arr[0];
                }
                if($arr[1] !="" || $arr[1] != null){
                    $map['XMPY'] = $arr[1];
                }
                if($arr[2] !="" || $arr[2] != null){
                    $map['XBM'] = $arr[2];
                }

                if($arr[3] !="" || $arr[3] != null){
                    $time = strtotime($arr[3]);
                    $map['birthday'] = date('Y-m-d',$time);
                }
                if($arr[4] !="" || $arr[4] != null){
                    $map['XH'] = $arr[4];
                }
                if($arr[5] !="" || $arr[5] != null){
                    $pp['LXDH'] = $arr[5];
                }
                if($arr[6] !="" || $arr[6] != null){
                    $pp['XM'] = $arr[6];
                }
                if($arr[7] !="" || $arr[7] != null){
                    $map['relation'] = $arr[7];
                }
                if($arr[8] !="" || $arr[8] != null){
                    $uu['SJHM'] = $arr[8];
                }

                if($map != null){
                    $map['school_id'] = $_SESSION['school_id'];
                    $pp['school_id'] = $_SESSION['school_id'];
                    $user = M('user');
                    $u['SJHM'] = $uu['SJHM'];
                    $userlist = $user->where($u)->find();

                    if(empty($userlist)){
                        $pp['user_id'] = $this->_create_user('parent','','',$uu['SJHM']);
                        $pp['FBSJ']=time();
                        $pid = $parent->add($pp);
                        $map['parent_id'] = $pid;
                        $map['class_id'] = $class_id;
                        $stu->add($map);
                    }else{
                    	$parentinfo =loadinfo($userlist['id'],2);
			            if (empty($parentinfo))
			            {
			                $dataparent['XM']= $pp['XM'];
			                $dataparent['LXDH'] =  $pp['LXDH'];
			                $dataparent['user_id'] = $userlist['id'];
			                $datauser['is_parent']=1;
			                $user->where('id='.$userlist['id'])->save($datauser);
			                $mparent= M('parent');
			                $mparent->add($dataparent);
			            }

                        $parentinfo =loadinfo($userlist['id'],2);

                        $maps['parent_id'] = $parentinfo['id'];
                        $maps['XM'] = $map['XM'];
                        $maps['class_id'] = $class_id;
                        $isnum = $stu->where($maps)->count();
                        $map['class_id'] = $class_id;
                        if($isnum >0){
                           $this->error('第'.($i).'行，'.$map['XM']."学生已存在");
                        }else{
                            $map['parent_id'] = $parentinfo['id'];
                            $sid = $stu->add($map);
<<<<<<< HEAD
                            add_log($_SESSION['schooler_id'],'添加 ID:'.$sid.'学生',2);
=======


                            //$mppower->add($dap);
                            $this->_addparentpower($sid,$map['parent_id'],$arr[7]);


                            add_log($_SESSION['teacher_id'],'添加 ID:'.$sid.'学生',2);
>>>>>>> 4d10520e2d379bccf159d357fbc2e5519f23428b

                        }
                    }
                }
            }
//          echo "<a href='".__URL__."/stu_import'>返回</a>";
            $this->success('导入成功');
        }else{
            $this->error("上传失败！");
        }
    }












    //学生方法结束



    //家长方法

    public function stu_parent()
    {
        //$id = $_REQUEST['id'];
        $id = $_REQUEST['id'];
        $this->assign('student_id',$id);
        $this->display();
    }
    public function stuparent_save()
    {
        $data =$this ->_post();
        if ($data['student_id'])
        {
            $mstudent= M('student');
            $studentinfo =$mstudent->where('id='.$data['student_id'])->find();
            if ($studentinfo['parent_id'])
            {
                $this->error('非法操作');
            }
            else
            {
                if ( !$data['parentXM'] or !$data['SJHM'])
                {
                    $this->error('信息填写不完整！');
                }
                $muser =M('user');
                $userinfo =$muser->where("SJHM ='".$data['SJHM']."'")->find();
                if (empty($userinfo))
                {
                    $dataparent['XM']= $data['parentXM'];
                    $dataparent['LXDH'] = $data['LXDH'];
                    $dataparent['XBM'] = $data['parentXBM'];
                    $dataparent['DZXX'] = $data['DZXX'];
                    $dataparent['QQ'] = $data['QQ'];
                    $dataparent['birthday'] = $data['parentbirthday'];
                    $dataparent['user_id'] = $this->_create_user('parent','','',$data['SJHM']);
                    $mparent= M('parent');
                    $data1['parent_id'] = $mparent->add($dataparent);
                }
                else
                {

                    $parentinfo =loadinfo($userinfo['id'],2);
                    if (empty($parentinfo))
                    {
                        $dataparent['XM']= $data['parentXM'];
                        $dataparent['LXDH'] = $data['LXDH'];
                        $dataparent['XBM'] = $data['parentXBM'];
                        $dataparent['DZXX'] = $data['DZXX'];
                        $dataparent['QQ'] = $data['QQ'];
                        $dataparent['birthday'] = $data['parentbirthday'];
                        $dataparent['user_id'] = $userinfo['id'];
                        $datauser['is_parent']=1;
                        $muser->where('id='.$userinfo['id'])->save($datauser);
                        $mparent= M('parent');
                        $mparent->add($dataparent);

                    }
                    $parentinfo =loadinfo($userinfo['id'],2);
                    $data1['parent_id'] = $parentinfo['id'];


                }
                $data1['relation'] = $data['relation'];
                $mstudent->where('id='.$data['student_id'])->save($data1);
                add_log($_SESSION['schooler_id'],'设置 ID:'.$data['student_id'].'学生的家长',2);

                $this ->success('操作成功',__URL__.'/stu_man');
            }
        }
        else
        {
            $this->error('非法操作');
        }
    }

    public function parent_man()
    {
        if (!empty($_REQUEST['name']))
        {
            $map_parent['student.XM'] = array('like','%'.$_REQUEST['name'].'%');
        }
        if (!empty($_REQUEST['class_id']))
        {
            $map_parent['student.class_id'] = $_REQUEST['class_id'];
        }

        if (!empty($_REQUEST['parentname']))
        {
            $map_parent['parent.XM'] = array('like','%'.$_REQUEST['parentname'].'%');
        }
        if (!empty($_REQUEST['tel']))
        {
            $map_parent['LXDH'] = array('like','%'.$_REQUEST['tel'].'%');

        }
        if (!empty($_REQUEST['phone']))
        {
                $mappp['SJHM'] = array('like','%'.$_REQUEST['phone'].'%');
                $muser =M('user');
                $userllist =$muser->field('id')->where($mappp)->select();
                foreach ($userllist as $ul) {
                    $uidlist .= $ul['id'].',';
                }
                $uidlist .= '0';
                $map_parent['user_id'] = array('in',$uidlist);
        }

        $map_parent['student.school_id']=$_SESSION['school_id'];

        $mparent =M('parent');
        import('ORG.Util.Page');// 导入分页类
        
        $count      = $mparent->where($map_parent)->join('parent_power on parent.id = parent_power.parent_id')->join('student on student.id = parent_power.student_id')->where($map_parent)->count();// 查询满足要求的总记录数
        
        $Page       = new Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输
        $parentlist = $mparent->field('parent.*,student.XM as studentname,student.class_id as class_id')->join('parent_power on parent.id = parent_power.parent_id')->join('student on student.id = parent_power.student_id')->where($map_parent)->limit($Page->firstRow.','.$Page->listRows)->select();
        
        $this->assign('attencelist',$attencelist);
        $this->assign('page',$show);// 赋值分页输出
        $Page->parameter   .=   "$class_id=".urlencode($_REQUEST['class_id']).'&';
        $Page->parameter   .=   "$tel=".urlencode($_REQUEST['tel']).'&';
        $Page->parameter   .=   "$phone=".urlencode($_REQUEST['phone']).'&';
        $Page->parameter   .=   "$parentname=".urlencode($_REQUEST['parentname']).'&';
        $Page->parameter   .=   "$name=".urlencode($_REQUEST['name']).'&';

        //echo $mparent->getlastsql();
        $this->assign('parentlist',$parentlist);
        $this->display();
        //print_r($parentlist);



    }




    public function parent_man1()
    {
        $map['school_id']=$_SESSION['school_id'];

        if (!empty($_REQUEST['name']))
        {
            $map['XM'] = array('like','%'.$_REQUEST['name'].'%');
        }
        if (!empty($_REQUEST['class_id']))
        {
            $map['class_id'] = $_REQUEST['class_id'];
        }
        $map['parent_id'] = array('neq','');

        if (!empty($_REQUEST['tel']) or !empty($_REQUEST['parentname']) or !empty($_REQUEST['phone']))
        {
            $mapp['LXDH'] = array('like','%'.$_REQUEST['tel'].'%');
            $mapp['XM'] = array('like','%'.$_REQUEST['parentname'].'%');
            if (!empty($_REQUEST['phone']))
            {
                $mappp['SJHM'] = array('like','%'.$_REQUEST['phone'].'%');
                $muser =M('user');
                $userllist =$muser->field('id')->where($mappp)->select();
                foreach ($userllist as $ul) {
                    $uidlist .= $ul['id'].',';
                }
                $uidlist .= '0';
                $mapp['user_id'] = array('in',$uidlist);
            }
            $mparent =M('parent');
            $parentidlist = $mparent->field('id') ->where($mapp)->select();
            foreach ($parentidlist as $pl) {
                $idlist .=$pl['id'].',';
                # code...
            }
            $idlist .= '0';

            $map['parent_id'] = array('in',$idlist);
        }



        $mstudent= M('student');

        $this->_listpage($mstudent, $map, $sortBy = '', $asc = false , 0 , $db = '',$listRows='15');
        $this->display();
    }

    public function parent_edit()
    {
        $id =$this->_get('id');
        $mstudent =M('student');
        $studentinfo =$mstudent ->find($id);
        $parentsex = getinfo($studentinfo['parent_id'],'parent','XBM');
        $this->assign('parentsex',$parentsex);
        $this->assign('studentinfo',$studentinfo);
        $this->display();

    }


    public function parent_save()
    {
        $data = $this->_post();
        if (!$data['parentXM'] or !$data['XM'] or !$data['SJHM']  or !$data['class_id'])
        {
            $this->error('信息填写不完整！');
        }
        $muser =M('user');
        $userinfo =$muser->where("SJHM ='".$data['SJHM']."' ")->find();
        if (empty($userinfo))
        {
            if($_FILES['pic']['name'] != null){
                $upload = $this->uploadimg('student/');
                $pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
                $dataparent['pic'] = $pic;
            }
            $dataparent['XM']= $data['parentXM'];
            $dataparent['LXDH'] = $data['LXDH'];
            $dataparent['XBM'] = $data['parentXBM'];
            $dataparent['DZXX'] = $data['DZXX'];
            $dataparent['QQ'] = $data['QQ'];
            $dataparent['birthday'] = $data['parentbirthday'];
            $dataparent['user_id'] = $this->_create_user('parent','','',$data['SJHM']);
            $mparent= M('parent');
            $data['parent_id'] = $mparent->add($dataparent);
        }
        else
        {
            $parentinfo =loadinfo($userinfo['id'],2);
            if (empty($parentinfo))
            {
                $dataparent['XM']= $data['parentXM'];
                $dataparent['LXDH'] = $data['LXDH'];
                $dataparent['XBM'] = $data['parentXBM'];
                $dataparent['DZXX'] = $data['DZXX'];
                $dataparent['QQ'] = $data['QQ'];
                $dataparent['birthday'] = $data['parentbirthday'];
                $dataparent['user_id'] = $userinfo['id'];
                $mparent= M('parent');
                $datauser['is_parent']=1;
                $muser->where('id='.$userinfo['id'])->save($datauser);
                $mparent->add($dataparent);

            }
            $parentinfo =loadinfo($userinfo['id'],2);
            $data['parent_id'] = $parentinfo['id'];
        }
        add_log($_SESSION['schooler_id'],'添加 ID:'.$data['parent_id'].'家长',2);

        $data['school_id'] =$_SESSION['school_id'];
        $mstudent =M('student');
        $mstudent ->add($data);
        $this ->success('操作成功',__URL__.'/stu_man');
    }


    public function parent_update()
    {
        $data = $this->_post();
        if (!$data['XM']  )
        {
            $this->error('信息填写不完整！');
        }
        $mparent =M('parent');
        $map['id'] = $data['parent_id'];
        $parentinfo =$mparent->where($map)->find();

        //$map['SJHM']= $data['SJHM'];
        $muser =M('user');
        $userinfo = $muser->where("SJHM='".$data['SJHM']."' and id != ".$parentinfo['user_id'])->find();
        if (!empty($userinfo))
        {
            $this->error('手机号码已存在！');
        }
        else
        {
            $data1['SJHM']=$data['SJHM'];

            if($_FILES['pic']['name'] != null){
                $upload = $this->uploadimg('student/');
                $pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
                //$data['pic'] = $pic;
                $data['pic'] = ltrim($pic,'/');
            }


            $mparent -> where($map)->save($data);
            $muser ->where('id='.$parentinfo['user_id'])->save($data1);
            add_log($_SESSION['schooler_id'],'修改 ID:'.$parentinfo['id'].'家长',2);

        }
        // echo $mparent->getlastsql();
        $this ->success('操作成功',__URL__.'/parent_man');
    }


    public function parent_import(){
        $this->display();
    }

    public function parent_doimport(){
        $class_id = $this->_isempty($_POST['class_id'],'请选择班级');
        $dir = dirname(_FILE_);//获得编译文件当前目录
        $dir = str_replace("\\", "/", $dir)."/";
        $time = time();
        $filename = 'parent'.$time.'.xls';
        $path = $dir.$filename;
        $path = "./excel/".$filename;
        $result = move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path);
        if ($result){
            Vendor('excel_reader2');//调用第三方类，可以是require_once('指向reader.php文件所在的路径')
            $data = new Spreadsheet_Excel_Reader();
            $data->setOutputEncoding('utf-8');

            error_reporting(E_ALL ^ E_NOTICE);
            $data->read($path);

            $stu = M('student');
            $parent = M('parent');
            for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
                $arr = array();
                for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                    $arr[] = $data->sheets[0]['cells'][$i][$j];
                }
                mysql_query("set names 'utf-8'");

                if($arr[0] !="" || $arr[0] != null){
                    $map['XM'] = $arr[0];
                }
                if($arr[1] !="" || $arr[1] != null){
                    $map['XBM'] = $arr[1];
                }
                if($arr[2] !="" || $arr[2] != null){
                    $time = date('Y-m-d',strtotime($arr[2]));
                    $map['birthday'] = $time;
                }

                if($arr[3] !="" || $arr[3] != null){
                    $uu['SJHM'] = $arr[3];
                }
                if($arr[4] !="" || $arr[4] != null){
                    $map['DZXX'] = $arr[4];
                }
                if($arr[5] !="" || $arr[5] != null){
                    $map['qq'] = $arr[5];
                }
                if($arr[6] !="" || $arr[6] != null){
                    $xs['XM'] = $arr[6];
                }
                if($arr[7] !="" || $arr[7] != null){
                    $xs['relation'] = $arr[7];
                }


                if($map != null){
                    $map['school_id'] = $_SESSION['school_id'];
                    $pp['school_id'] = $_SESSION['school_id'];
                    $user = M('user');
                    $u['SJHM'] = $uu['SJHM'];
                    $userlist = $user->where($u)->find();

                    if(empty($userlist)){
                        $map['user_id'] = $this->_create_user('parent','','',$uu['SJHM']);
                        $map['FBSJ']=time();

                        $pid = $parent->add($map);
                        $ss['parent_id'] = $pid;
                        $ss['XM'] = $xs['XM'];
                        $ss['relation'] = $xs['relation'];
                        $ss['class_id'] = $class_id;
                        $stu->add($ss);
                        add_log($_SESSION['schooler_id'],'添加 ID:'.$pid.'家长',2);

                    }else{
                        $parentinfo =loadinfo($userlist['id'],2);
                        if($parentinfo['id'] != null){
                            $ss['parent_id'] = $parentinfo['id'];
                            $ss['XM'] = $xs['XM'];
                            $ss['class_id'] = $class_id;
                            $isnum = $stu->where($ss)->count();

                            if($isnum >0){
                                $this->error('第'.($i).'行，'.$ss['XM'].'学生已存在');
                            }else{
                                $ss['parent_id'] = $parentinfo['id'];
                                $stu->add($ss);
                                add_log($_SESSION['schooler_id'],'添加 ID:'.$parentinfo['id'].'家长',2);

                            }
                        }else{
                            $pid = $parent->add($map);
                            $ss['parent_id'] = $pid;
                            $ss['XM'] = $xs['XM'];
                            $ss['relation'] = $xs['relation'];
                            $ss['class_id'] = $class_id;
                            $stu->add($ss);
                            add_log($_SESSION['schooler_id'],'添加 ID:'.$pid.'家长',2);

                        }
                    }
                }
            }
            $this->success('添加成功');
        }else{
            $this->error("上传失败！");
        }
    }








    //家长方法
    //class方法

    public function school_man(){
        $id = $this->_isempty($_SESSION['school_id'],'ID不能为空');
        $m = M('school');
        $list = $m->find($id);


       	$quyu = $list['quyu'];
		$quyu = explode('-',$quyu);
		$this->assign('quyu0',$quyu[0]);
		$this->assign('quyu1',$quyu[1]);
		$this->assign('quyu2',$quyu[2]);

        $this->assign('list',$list);
        $this->display();
    }
    function schooledit(){
        $id = $this->_isempty($_POST['id'],'ID不能为空');
        $xxmc = $this->_isempty($_POST['XXMC'],'学校名称不能为空');
        $lxdh = $this->_isempty($_POST['LXDH'],'联系电话不能为空');

		$sheng = $this->_isempty($_POST['sheng'],'请选择学校所在区域');
		$shi = $this->_isempty($_POST['shi'],'请选择学校所在区域');
		$qu = $this->_isempty($_POST['qu'],'请选择学校所在区域');


        $m = M('school');
        $map0['id'] = $id;
        $map0['XXMC'] = $xxmc;
        $map0['XXYWMC'] = $_POST['XXYWMC'];
        $map0['XXDM'] = $_POST['XXDM'];
        $map0['XXJJ'] = $_POST['XXJJ'];
        $map0['XXDZ'] = $_POST['XXDZ'];
        $map0['quyu'] = $_POST['quyu'];
        $map0['XZXM'] = $_POST['XZXM'];
        $map0['LXDH'] = $lxdh;
        $map0['ZYDZ'] = $_POST['ZYDZ'];
        $map0['CZDH'] = $_POST['CZDH'];
        $map0['DZXX'] = $_POST['DZXX'];
		$map0['quyu'] = $_POST['sheng'].'-'.$_POST['shi'].'-'.$_POST['qu'];

        $m = M('school');
        $m->save($map0);

        $this->assign('jumpUrl',__URL__.'/school_man/');
        $this->assign('waitSecond','3');
        $this->success("修改成功");
    }


    public function class_man()
    {
        $map['school_id']=$_SESSION['school_id'];
        if (!empty($_REQUEST['name']))
        {
            $map['class_name'] = array('like','%'.$_REQUEST['name'].'%');
        }
        if (!empty($_REQUEST['teachername']))
        {
            $mapte['XM'] = array('like','%'.$_REQUEST['teachername'].'%');
            $mteacher=M('teacher');
            $teacherlist =$mteacher ->where($mapte)->select();
            foreach ($teacherlist as $tl) {
                $techeridlist .= $tl['id'].',';
                # code...
            }
            $techeridlist =rtrim($techeridlist,',');
            $map['teacher_id'] = array('in',$techeridlist);

        }

        $mclass = M('class');
        $this->_listpage($mclass, $map, $sortBy = '', $asc = false , 0 , $db = '',$listRows='15');
        $this->display();
    }
    public function class_add()
    {
        $map['school_id']=$_SESSION['school_id'];
        $mteacher=M('teacher');
        $teacherlist = $mteacher->where($map)->order('CONVERT(XM USING gbk)')->select();
        $this->assign('teacher',$teacherlist);
        $this->display();
    }
    public function class_edit()
    {
        $id =$_REQUEST['id'];
        $mclass =M('class');
        $classinfo = $mclass->find($id);
        $map['school_id']=$_SESSION['school_id'];
        $mteacher=M('teacher');
        $teacherlist = $mteacher->where($map)->order('CONVERT(XM USING gbk)')->select();
        $this->assign('teacher',$teacherlist);
        $this->assign('classinfo',$classinfo);
        $this->assign('id',$id);

        $this->display();
    }
    public function class_save()
    {
        $id =$this ->_insert('Class');
        if ($id)
        {
            add_log($_SESSION['schooler_id'],'添加 ID:'.$id.'班级',2);

             $this ->success('操作成功',__URL__.'/class_man');
        }
    }

    public function class_update()
    {
        $id =$_REQUEST['id'];
        if ($this ->_update('Class'))
        {
            add_log($_SESSION['schooler_id'],'修改 ID:'.$id.'班级',2);

             $this ->success('操作成功',__URL__.'/class_man');
        }
    }
    public function class_del()
    {
        $id = $this->_loadids();


        $map['class_id'] = array('in',$id);
        $map1['id'] =array('in',$id);


        $mstudent=M('student');
        $mclass_proclamation =M('class_proclamation');
        $mschedule=M('schedule');
        $count1 = $mstudent->where($map)->count();
        $count2 = $mclass_proclamation->where($map)->count();
        $count3 = $mschedule->where($map)->count();
        if ($count1>0)
        {
            $this->error('不可删除，存在该班的学生');
        }
        if ($count2>0)
        {
            $this->error('不可删除，存在该班的公告');
        }
        if ($count3>0)
        {
            $this->error('不可删除，学生有存在的课程');
        }


        $map['class_id'] = array('in',$id);
        $map1['id'] =array('in',$id);
        if ($count1==0 and $count2==0 and $count3==0)
        {
            $mclass=M('class');
            $mclass->where($map1)->delete();
            add_log($_SESSION['schooler_id'],'删除 ID:'.$id.'班级',2);

            $this->success('删除成功');
        }

    }


    public function class_doimport(){
        $dir = dirname(_FILE_);//获得编译文件当前目录
        $dir = str_replace("\\", "/", $dir)."/";
        $time = time();
        $filename = 'class'.$time.'.xls';
        $path = $dir.$filename;
        $path = "./excel/".$filename;
        $result = move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path);
        if ($result){
            Vendor('excel_reader2');//调用第三方类，可以是require_once('指向reader.php文件所在的路径')
            $data = new Spreadsheet_Excel_Reader();
            $data->setOutputEncoding('utf-8');

            error_reporting(E_ALL ^ E_NOTICE);
            $data->read($path);
            $teacher = M('teacher');
            $user = M('user');
            for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
                $arr = array();
                for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                    $arr[] = $data->sheets[0]['cells'][$i][$j];
                }
                mysql_query("set names 'utf-8'");

                if($arr[0] !="" || $arr[0] != null){
                    $lel['level_name'] = $arr[0];
                }
                if($arr[1] !="" || $arr[1] != null){
                    $map['class_name'] = $arr[1];
                }
                if($arr[2] !="" || $arr[2] != null){
                    $map['class_code'] = $arr[2];
                }

                if($arr[3] !="" || $arr[3] != null){
                    $tt['XM'] = $arr[3];
                }

                $tt['school_id']=$_SESSION['school_id'];
                if($map != null){
                    $map['create_time'] = time();

                    $level = M('level');
                    $lellist = $level->where($lel)->find();
                    if($lellist != null){
                        $map['level_id'] = $lellist['id'];

                        $tea = M('teacher');
                        // if (!empty($arr[3]))
                        // {
                        //     $tealist = $tea->where($tt)->find();
                        //     if (empty($tealist))
                        //     {
                        //         $this->error('第'.($i).'行，'.$tt['XM']."不存在");
                        //     }
                        //     $map['teacher_id'] = $tealist['id'];
                        // }


                        $map['school_id'] = $_SESSION['school_id'];
                        $class = M('class');
                        $cid =$class->add($map);
                        add_log($_SESSION['schooler_id'],'添加 ID:'.$cid.'班级',2);

                    }else{
//                      echo $lel['level_name']."没有此年级";
                        $this->error('第'.($i).'行，'.$lel['level_name']."没有此年级");
                    }
                }
            }
//          echo "<a href='".__URL__."/class_import'>返回</a>";
            $this->success('导入成功');
        }else{
            $this->error("上传失败！");
        }
    }












    //sbject方法
    public function subject_man()
    {
        $map['school_id']=$_SESSION['school_id'];
        if (!empty($_REQUEST['name']))
        {
            $map['KCMC'] = array('like','%'.$_REQUEST['name'].'%');
        }
        $msubject = M('subject');
        $this->_listpage($msubject, $map, $sortBy = '', $asc = false , 0 , $db = '',$listRows='15');
        $this->display();
    }

    public function subject_save()
    {
        $id =$this ->_insert('Subject');
        if ($id)
        {
            add_log($_SESSION['schooler_id'],'添加 ID:'.$id.'科目',2);

             $this ->success('操作成功',__URL__.'/subject_man');
        }
    }

    public function subject_edit(){
		$id = $this->_isempty($_GET['id'],'ID不能为空');
		$m = M('subject');
		$list = $m->find($id);
		$this->assign('list',$list);
    	$this->display();
    }

	public function subject_doedit(){
		$id = $this->_isempty($_POST['id'],'ID不能为空');
		$m = M('subject');
		$data['id'] = $id;
		$data['KCMC'] = $_POST['KCMC'];
		$data['KCJJ'] = $_POST['KCJJ'];
		if($m->save($data)){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}



    //subject方法

    //课程表方法
    public function lesson_time()
    {
        $map['school_id']=$_SESSION['school_id'];
        $mlesson_type =M('lesson_type');
        $lessonlist =$mlesson_type->where($map)->order('num asc')->select();
        $this->assign('lessonlist',$lessonlist);
        $this->display();

    }
    public function lesson_time1()
    {
        $map['school_id']=$_SESSION['school_id'];
        $mlesson_type =M('lesson_type');
        $lessonlist =$mlesson_type->where($map)->order('num asc')->select();
        if (empty($lessonlist))
        {
            $this->assign('p','1');
        }
        $this->assign('lessonlist',$lessonlist);
        $this->display();
    }
    public function lessontype_del()
    {
        $id=$_REQUEST['id'];
        $mlesson_type =M('lesson_type');
        $mlesson =M('lesson');
        if ($mlesson->where('lesson_type_id='.$id)->count()>0)
        {
            $this->error('课次使用中不能删除');
        }
        else
        {
            add_log($_SESSION['schooler_id'],'删除 ID:'.$id.'课次',2);
            $mlesson_type->delete($id);
            $this->redirect('/School/lesson_time1');
        }
    }

     public function lessontype_save1()
    {
        $data =$this->_post();
        $mlesson_type =M('lesson_type');
        $nums = count($data['type_name']);
        echo $num;
        for ($i=0; $i < $nums; $i++) {
            # code...
            $datalt='';
            if (empty($data['id'][$i]))
            {
                $datalt['type_name']=$data['type_name'][$i];
                $datalt['starttime']=$data['shour'][$i].':'.$data['smin'][$i];
                $datalt['endtime']=$data['ehour'][$i].':'.$data['emin'][$i];
                $datalt['school_id']=$_SESSION['school_id'];
                $sid = $mlesson_type->add($datalt);
                add_log($_SESSION['schooler_id'],'添加 ID:'.$sid.'课次',2);

            }
            else
            {
                $datalt['type_name']=$data['type_name'][$i];
                $datalt['starttime']=$data['shour'][$i].':'.$data['smin'][$i];
                $datalt['endtime']=$data['ehour'][$i].':'.$data['emin'][$i];
                $mlesson_type->where('id='.$data['id'][$i])->save($datalt);
                add_log($_SESSION['schooler_id'],'修改 ID:'.$data['id'][$i].'课次',2);


            }
        }
        $$mlesson_type =M('lesson_type');
        $lessonlist = $mlesson_type ->field("id,num,REPLACE(starttime,':','')"." as time ")->where($map)->order('(time+0) asc')->select();
            $j=1;
        foreach ($lessonlist as $ll) {
            $data['num'] = $j;
            $mlesson_type->where('id='.$ll['id'])->save($data);
            $j++;
                # code...
        }
        $this ->success('操作成功');

    }
    public function lessontype_save()
    {
        $map['school_id']=$_SESSION['school_id'];


        if ($this ->_insert('Lesson_type'))
        {
            $mlesson_type =M('lesson_type');
            $lessonlist = $mlesson_type ->field("id,num,REPLACE(starttime,':','')"." as time ")->where($map)->order('(time+0) asc')->select();
            $i=1;
            foreach ($lessonlist as $ll) {
                $data['num'] = $i;
                $mlesson_type->where('id='.$ll['id'])->save($data);
                $i++;
                # code...
            }
            $this ->success('操作成功');
        }
    }

    private function _lesson_mn()
    {
        $map['school_id']=$_SESSION['school_id'];
        if (!empty($_REQUEST['class_id']))
        {
            $map['class_id'] = $_REQUEST['class_id'];
            $this->assign('class_id',$map['class_id']);
        }
        $msubject =M('subject');
        $mteacher =M('teacher');
        $subject =$msubject->where($map)->select();
        $teacher = $mteacher->where($map)->select();
        $this->assign('subject',$subject);
        $this->assign('teacher',$teacher);
        $this ->_loadlessontype();


    }
    public function lesson_man()
    {
        $this->_lesson_mn();
        $this->display();
    }
    public function lesson_class()
    {
        $this->_lesson_mn();
        $this->display();
    }
    public function lesson_year()
    {
        $map['school_id']=$_SESSION['school_id'];
        $school=M('school');
        $level =M('level');
        $class =M('class');
        $schoolinfo =$school ->find($map);
        $levellist =$level->where('school_type_id='.$schoolinfo['school_type_id'])->select();
        $this->assign('levellist',$levellist);

        if (!empty($_REQUEST['level_id']))
        {
            $map['level_id'] = $_REQUEST['level_id'];
            $classlist = $class->where($map)->select();
            $this->assign('classlist',$classlist);
            $this ->_loadlessontype();
            $timeint =time();
            $week = date("w",$timeint);
            if ($week==0)
            {
                $week =7;
            }
            $this->assign('week',$week);


        }

        $this->display();
    }
    public function lesson_school()
    {
       $map['school_id']=$_SESSION['school_id'];
        $school=M('school');
        $level =M('level');
        $class =M('class');
        $classlist = $class->where($map)->select();
        $classlist = $class->where($map)->select();
        $this->assign('classlist',$classlist);
        $week = loadweek($_REQUEST['time']);
        $this ->_loadlessontype();
        $this->assign('week',$week);

        $this->display();


    }

    public function lesson_save()
    {
        $map['school_id']=$_SESSION['school_id'];
        $data= $this->_post();
        $lesson_type =M('lesson_type');
        $lesson =M('lesson');
        $lessontylist =$lesson_type->where('school_id='.$map['school_id'])->select();
        $schedule =M('schedule');
        $scheduleinfo =$schedule->where('class_id='.$data['class_id'].' and XXZT=1')->find();
        if (empty($scheduleinfo))
        {
            $datasc['class_id']=$data['class_id'];
            $datasc['XXZT']=1;
            $datasc['FBSJ']=time();
            $schedule_id =$schedule->add($datasc);//插入课程表
        }
        else
        {
            $schedule_id=$scheduleinfo['id'];
        }

        $map['school_id']=$_SESSION['school_id'];

        for ($i=1; $i <7; $i++) {
            foreach ($lessontylist as $ls) {
                $datales='';
                $issave = $this->_teachercanlesson($data['teacherkc_'.$i.'_'.$ls['id']],$ls['id'],$i);
                if ($issave==1)
                {
                    if (!empty($data['lessonkc_'.$i.'_'.$ls['id']]))//判断有否课程
                    {
                        $datales['subject_id'] = $data['subjectkc_'.$i.'_'.$ls['id']];
                        $datales['teacher_id'] = $data['teacherkc_'.$i.'_'.$ls['id']];
                        $lesson->where('id='.$data['lessonkc_'.$i.'_'.$ls['id']])->save($datales);

                    }
                    else
                    {
                        if ($data['add_'.$i.'_'.$ls['id']])
                        {

                            $datales['subject_id'] = $data['subjectkc_'.$i.'_'.$ls['id']];
                            $datales['teacher_id'] = $data['teacherkc_'.$i.'_'.$ls['id']];
                            if (!empty($datales['subject_id']) and !empty($datales['teacher_id']))
                            {

                                $datales['week_day'] =$i;
                                $datales['lesson_type_id']=$ls['id'];
                                $datales['schedule_id']=$schedule_id;
                                $lesson->add($datales);
                            }

                        }
                    }
                }
            }


        }
        add_log($_SESSION['schooler_id'],'编排课程',2);

       $this ->success('操作成功,出现课程编排冲突的将不会被保存',__URL__.'/lesson_man/class_id/'.$data['class_id']);


    }
    public function _teachercanlesson($teacher,$lesson_type_id,$week_day)
    {
        $map['teacher_id'] = $teacher;
        $map['lesson_type_id'] = $lesson_type_id;
        $map['week_day'] = $week_day;
        $map['XXZT']=1;
        $mlesson=M('lesson');

        if ($mlesson->join('schedule on schedule.id=lesson.schedule_id')->where($map)->count()>0)
        {
            //echo $mlesson->getlastsql();
            return 0;
        }
        else
        {
            //echo $mlesson->getlastsql();
            //exit;
            return 1;
        }

    }
    public function teacher_class()
    {
        $map['school_id']=$_SESSION['school_id'];
        $class_id= $_REQUEST['class_id'];
        $teacher_name= $_REQUEST['teacher_name'];
        $subject_id= $_REQUEST['subject_id'];
        $this->assign('class_id',$class_id);
		if($_REQUEST['dosubmit']){
			if(!$class_id){
				$this->assign('jumpUrl',__APP__.'/School/teacher_class/');
				$this->error("请选择班级!");
			}
		}
        if (!empty($_REQUEST['time']))
        {
            $week = loadweek($_REQUEST['time']);
        }

        $msubject =M('subject');
        $subject =$msubject->where($map)->select();
        $this ->_loadlessontype();
        $this->assign('week',$week);
        $this->assign('teacher_name',$teacher_name);
        $this->assign('subject_id',$subject_id);
        $this->assign('subject',$subject);
        $this->display();

    }

    //课程表方法
    //   教师模块
    public function teacher_man(){
        if($_REQUEST['username'] !=null && $_REQUEST['username'] != "" ){
            $user = M('user');
            $map['username'] = array('like','%'.$_REQUEST[username].'%');
            $userlist = $user->where($map)->select();
            foreach($userlist as $k=>$l){
                $ids[$k] = $l['id'];
            }
            $data['user_id'] = array('in',$ids);
        }
        if($_REQUEST['iswork'] !=null && $_REQUEST['iswork'] != "" ){
            $data['iswork'] = $_REQUEST['iswork'];
        }
        if($_REQUEST['XM'] != null && $_REQUEST['XM'] != ""){
	   //if (!empty($_REQUEST['XM'])){
            $data['XM'] = array('like','%'.$_REQUEST['XM'].'%'); ;
        }
        if($_REQUEST['subject_id'] != null && $_REQUEST['subject_id'] != ""){
            $data['subject_id'] = array('like','%'.$_REQUEST['subject_id'].'%');
        }

        $data['school_id'] = $_SESSION['school_id'];
        $m = M('teacher');
        $this->_listpage($m,$data);
        $sublist = $this->school_subject();
        $this->assign('sublist',$sublist);
        $this->display();
    }


    public function teacher_add(){
        $school_id = $_SESSION['school_id'];
        $this->assign('school_id',$school_id);
        $subject = $this->getsubject();
        $this->assign('subject',$subject);
        $this->display();
    }


    function teacher_doadd(){
        $sid = $this->_isempty($_SESSION['school_id'],'请选择学校');
        $XM = $this->_isempty($_POST['XM'],'请填写姓名');
        $SJHM = $this->_isempty($_POST['SJHM'],'手机不能为空');
        $username = $this->_isempty($_POST['username'],'账号不能为空');
        $password = $this->_isempty($_POST['password'],'密码不能为空');

//      $subjectid = $this->_isempty($_POST['subject_id'],'科目不能为空');
//      $classid = $this->_isempty($_POST['class_id'],'班级不能为空');
//      $powerid = $this->_isempty($_POST['power_id'],'管理班级');

        $subjectid = $_POST['subject_id'];
        $classid = $_POST['class_id'];
        $powerid = $_POST['power_id'];


        $map1['username'] = $username;
        $map1['password'] = $password;
        $map1['is_teacher'] = 1;
        // $map1['is_school'] = 0;
        // $map1['is_parent'] = 0;
        $map1['register_time'] = time();
        $map1['SJHM'] =$SJHM;
        $map1['password_des'] = encrypt($password);
//      $map1['school_id'] = $sid;
//      $map1['admin_id'] = $_SESSION['adminid'];
        $map1['status'] = 0;


        $map1['shenhe'] = $_REQUEST['iswork'];

        $map1['audit_opinion'] = $_POST['audit_opinion'];
        $map1['vcode']=randchar();
		//创建时间
		//$map1['addtime'] = time();


        $map2['XM'] = $_POST['XM'];
        $map2['XMPY'] = $_POST['XMPY'];
        $map2['XBM'] = $_POST['XBM'];
        if($_FILES['pic']['name'] != null){
            $upload = $this->uploadimg('teacher/');
            $pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
            $map2['pic'] = ltrim($pic,'/');
        }
        $map2['teacher_no'] = $_POST['teacher_no'];
        $map2['LXDH'] = $_POST['LXDH'];
        $map2['LXDZ'] = $_POST['LXDZ'];
        $map2['intro'] = $_POST['intro'];
        $map2['iswork'] = $_POST['iswork'];
        $map2['DZXX'] = $_POST['DZXX'];
        $map2['QQ'] = $_POST['QQ'];
        $map2['school_id'] = $sid;

        if(count($subjectid)>0){
            $subjectid = implode(',',$subjectid);
            $map2['subject_id'] = $subjectid;
        }


        $teacherid = $this->addteacherinfo($sid,$map1,$map2);
        if($teacherid >0){
//          add_log($_SESSION['adminid'],'添加用户',2);
//          add_log($_SESSION['adminid'],'添加老师');
            add_log($_SESSION['schooler_id'],'添加 ID:'.$teacherid.'教师',2);

            $this->updateclass($classid,$teacherid);
            $this->updateteacherpower($powerid,$teacherid);

            $this->assign('jumpUrl',__APP__.'/School/teacher_man/');
            $this->assign('waitSecond','1');
            $this->success("添加成功");
        }
    }



    function teacher_edit(){
        $id = $this->_isempty($_GET['id'],'ID不能为空');
        $teacher = M('teacher');
        $data['id'] = $id;
        $tlist = $teacher->where($data)->find();

        $m = M('user');
        $list = $m->find($tlist['user_id']);

        $this->assign('list',$list);

        $this->assign('tlist',$tlist);


        $subjectid = explode(',',$tlist['subject_id']);
        $subject = $this->_editsubject($tlist['school_id'],$subjectid);
        $class = $this->_editclass($tlist['school_id'],$tlist['id']);
        $power = $this->_editpower($tlist['school_id'],$tlist['id']);

        $this->assign('subject',$subject);
        $this->assign('class',$class);
        $this->assign('power',$power);
        $this->display();
    }

    function teacher_goto(){
        $id = $this->_isempty($_GET['id'],'ID不能为空');
		$m = M('user');
		$list = $m->where(array('id'=>$id))->find();
		$_SESSION['scode'] = C('SYSTEM_CODE');
		$_SESSION['susername_goto'] = $list['username'];
		$_SESSION['spassword_goto'] = $list['password'];
		//$_SESSION['suserid'] = $list['id'];
		$_SESSION['s_isgoto'] = 1;
		
		if($list['id'] != null){
				$t = M('teacher');
				$tt['user_id'] = $list['id'];
				$tlist = $t->where($tt)->find();
				// if($tlist == null){
				// 	$this->error('无此角色');
				// }
				$_SESSION['teacher_id'] = $tlist['id'];
				$_SESSION['school_id'] = $tlist['school_id'];

				$logid = add_log($tlist['id'],'登录 ID:'.$tlist['id'].'-'.$username,1);
				$_SESSION['logid'] = $logid;
		}
		$_SESSION['type'] = 'teacher';	
		
		$this->redirect('Teacher/index');
    }
	
	

    function teacher_doedit(){
        $id = $this->_isempty($_POST['id'],'ID 不能为空');
        $teacherid = $this->_isempty($_POST['teacherid'],'TID不能为空');

        $sid = $this->_isempty($_SESSION['school_id'],'请选择学校');
        $XM = $this->_isempty($_POST['XM'],'请填写姓名');
        $SJHM = $this->_isempty($_POST['SJHM'],'手机不能为空');

        //$endtime = $this->_isempty($_POST['endtme'],'截止日期不能为空');
        // $username = $this->_isempty($_POST['username'],'账号不能为空');
        // $password = $this->_isempty($_POST['password'],'密码不能为空');

        $subjectid = $_POST['subject_id'];
        $classid = $_POST['class_id'];
        $powerid = $_POST['power_id'];


        $map1['id'] = $id;
        // $map1['username'] = $username;
        // $map1['password'] = $password;
        $map1['is_teacher'] = 1;
        // $map1['is_school'] = 0;
        // $map1['is_parent'] = 0;
        // // $map1['register_time'] = time();
        $map1['SJHM'] =$SJHM;

        // $map1['password_des'] = encrypt($password);
        //$map1['school_id'] = $sid;
        // $map1['admin_id'] = $_SESSION['adminid'];
        // $map1['status'] = $_POST['status'];
        $map1['audit_opinion'] = $_POST['audit_opinion'];
       // $map1['endtime'] = strtotime($endtime);
        $m = M('user');


        if ($m->where("SJHM='".$SJHM."' and id!=".$id)->count()>0)
        {
            $this->error('手机号码已存在！');
        }
        $m->save($map1);

        $map2['id'] = $teacherid;
        $map2['XM'] = $_POST['XM'];
        $map2['XMPY'] = $_POST['XMPY'];
        $map2['XBM'] = $_POST['XBM'];
        if($_FILES['pic']['name'] != null){
            $upload = $this->uploadimg('teacher/');
            $pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
            $map2['pic'] = ltrim($pic,'/');
        }
        $map2['teacher_no'] = $_POST['teacher_no'];
        $map2['LXDH'] = $_POST['LXDH'];
        $map2['LXDZ'] = $_POST['LXDZ'];
        $map2['intro'] = $_POST['intro'];
        $map2['iswork'] = $_POST['iswork'];
        $datash['shenhe'] =$map2['iswork'];
        $m->where('id='.$id)->save($datash);

        $map2['DZXX'] = $_POST['DZXX'];
        $map2['QQ'] = $_POST['QQ'];
        $map2['school_id'] = $sid;

        if(count($subjectid)>0){
            $subjectid = implode(',',$subjectid);
            $map2['subject_id'] = $subjectid;
        }

        $tea = M('teacher');
        $tea->save($map2);

        $class = M('class');
        $set['teacher_id'] = null;
        $class->where('teacher_id='.$teacherid)->save($set);


        $tp = M('teacher_power');
        $whe['teacher_id'] = $teacherid;
        $tp->where($whe)->delete();



//      add_log($_SESSION['adminid'],'修改用户',2);
//      add_log($_SESSION['adminid'],'修改老师',2);
        $this->updateclass($classid,$teacherid);
        $this->updateteacherpower($powerid,$teacherid);
        add_log($_SESSION['schooler_id'],'修改 ID:'.$teacherid.'教师',2);

        $this->assign('jumpUrl',__APP__.'/School/teacher_man/');
        $this->assign('waitSecond','1');
        $this->success("修改成功");

    }


    function teacher_delete(){
        $id = $this->_isempty($_GET['id'],'ID不能为空');


        $lesson = M('lesson');
        $class = M('class');
        $student_work = M('student_work');
        $teacher_power = M('teacher_power');

        $map['teacher_id'] = $id;
        if($lesson->where($map)->find()){
            $this->error('课程中存在该老师数据');
        }

        if($class->where($map)->find()){
            $this->error('班级中存在该老师数据');
        }

        if($student_work->where($map)->find()){
            $this->error('学生作业中存在该老师数据');
        }

        if($teacher_power->where($map)->find()){
            $this->error('教师执教中存在该老师数据');
        }




        $tea = M('teacher');
        $tealist = $tea->find($id);
        $tea->delete($id);

        add_log($_SESSION['teacher_id'],'删除 ID:'.$id.'教师',2);

        $parent = M('parent');
        $pp['user_id'] = $tealist['user_id'];
        $plist = $parent->where($pp)->count();
        if($plist == 0){
            $m = M('user');
//          $userlist = $m->find($userid);
            $m->delete($tealist['user_id']);
        }

//      add_log($_SESSION['adminid'],'删除用户',2);
        $this->assign('jumpUrl',__APP__.'/School/teacher_man/');
        $this->assign('waitSecond','1');
        $this->success("删除成功");
    }


    function teacher_alldelete(){
        $ids = $this->_isempty($_POST['ids'],'ID不能为空');

        $lesson = M('lesson');
        $class = M('class');
        $student_work = M('student_work');
        $teacher_power = M('teacher_power');

        $tea = M('teacher');
        $m = M('user');
        $parent = M('parent');
        foreach($ids as $k=>$id){

            $map['teacher_id'] = $id;
            if($lesson->where($map)->find()){
                $this->error('课程中存在该老师数据');
            }

            if($class->where($map)->find()){
                $this->error('班级中存在该老师数据');
            }

            if($student_work->where($map)->find()){
                $this->error('学生作业中存在该老师数据');
            }

            if($teacher_power->where($map)->find()){
                $this->error('教师执教中存在该老师数据');
            }

            $tealist = $tea->find($id);
            add_log($_SESSION['schooler_id'],'删除 ID:'.$id.'教师',2);

            $pp['user_id']=$tealist['user_id'];
            $plist = $parent->where($pp)->count();
            if($plist == 0){
                $m->delete($tealist['user_id']);
            }
        }
        $data['id'] = array('in',$ids);
        $tea->where($data)->delete();
        $this->success("删除成功");
    }

    function teacher_import(){
        $this->display();
    }


    function teacher_doimport(){
        $school_id = $_SESSION['school_id'];

 		$dir = dirname(_FILE_);//获得编译文件当前目录
		$dir = str_replace("\\", "/", $dir)."/";
		$time = time();
		$filename = 'teacher'.$time.'.xls';
		$path = $dir.$filename;
		$path = "./excel/".$filename;
		$result = move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path);
		if ($result){
			Vendor('excel_reader2');//调用第三方类，可以是require_once('指向reader.php文件所在的路径')
			$data = new Spreadsheet_Excel_Reader();
			$data->setOutputEncoding('utf-8');

			error_reporting(E_ALL ^ E_NOTICE);
			$data->read($path);
			$teacher = M('teacher');
			$user = M('user');
			for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
			    $arr = array();
			    for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
			        $arr[] = $data->sheets[0]['cells'][$i][$j];
			    }
			    mysql_query("set names 'utf-8'");

				if($arr[0] !="" || $arr[0] != null){
					$map['XM'] = $arr[0];
				}
				if($arr[1] != "" || $arr[1] !=null){
					$map['XMPY'] = $arr[1];
				}

				if($arr[2] !="" || $arr[2] != null){
					$map['XBM'] = $arr[2];
				}
				if($arr[3] !="" || $arr[3] != null){
					$map['teacher_no'] = $arr[3];
				}

				if($arr[4] !="" || $arr[4] != null){
					$map['LXDH'] = $arr[4];
				}
				if($arr[5] !="" || $arr[5] != null){
					$pp['SJHM'] = $arr[5];
				}
				if($arr[6] !="" || $arr[6] != null){
					$map['LXDZ'] = $arr[6];
				}
				if($arr[7] !="" || $arr[7] != null){
					$map['intro'] = $arr[7];
				}
				if($arr[8] !="" || $arr[8] != null){
					if($arr[8] == '是'){
						$map['iswork'] =1;
					}else if($arr[8] == '否'){
						$map['iswork'] = 0;
					}
				}
				if($arr[9] !="" || $arr[9] != null){
					$pp['username'] = $arr[9];
				}
				if($arr[10] !="" || $arr[10] != null){
					$pp['password'] = $arr[10];
				}
				if($arr[11] !="" || $arr[11] != null){
					$map['DZXX'] = $arr[11];
				}
				if($arr[12] !="" || $arr[12] != null){
					$map['QQ'] = $arr[12];
				}

				if($arr[13] != "" || $arr[13] != null){
					$subject = explode('，',$arr[13]);
					$arrsubid = "";
					foreach($subject as $key=>$s){
						$mapcon['school_id'] = $school_id;
						$mapcon['KCMC'] = $s;
						$sub = M('subject');
						$subinfo = $sub->where($mapcon)->find();
						if($subinfo != null){
							$arrsubid.=$subinfo['id'].',';
						}else{
							$this->error('第'.($i).'行，科目'.$s.'不存在');
						}
					}
					$arrsubid.=0;
					$map['subject_id'] = $arrsubid;
				}


				if($map != null){
					$pp['is_teacher'] = 1;
					$pp['is_school'] = 0;
					$pp['is_parent'] = 0;
					$pp['register_time'] = time();
					$pp['password_des'] = encrypt($pp['password']);
					$pp['school_id'] = $school_id;
					$pp['status'] = 0;
					$pp['shenhe'] = 1;

					//创建时间
					$pp['addtime'] = time();

					$map['school_id'] = $school_id;

					$id = $this->addteacherinfo($school_id,$pp,$map);

					if($arr[14] != "" || $arr[14] != null){
						$class = M('class');
						$mapc['school_id'] = $school_id;
						$mapc['class_name'] = $arr[14];
						$classinfo = $class->where($mapc)->find();
						if($classinfo != null){
							$whe['id'] = $classinfo['id'];
							$set['teacher_id'] = $id;
							$class->where($whe)->save($set);
						}else{
							$this->error('第'.($i).'行，班级'.$arr[14].'不存在');
						}
					}

					if($arr[15] != "" || $arr[15] != null){
						$cname = explode('，',$arr[15]);
						$class = M('class');
						foreach($cname as $k=>$c){
							$mapc1['school_id'] = $school_id;
							$mapc1['class_name'] = $c;
							$classinfo = $class->where($mapc1)->find();
							if($classinfo != null){
								$ins['class_id'] = $classinfo['id'];
								$ins['teacher_id'] = $id;
								$tp = M('teacher_power');
								$tp->add($ins);
							}else{
								$this->error('第'.($i).'行，班级'.$c.'不存在');
							}
						}
					}

					if($id == 0){
						$this->error('第'.($i).'行，用户:'.$pp['username'].'导入失败');
					}
				}
			}
//			echo "<a href='".__URL__."/import'>返回</a>";
//			$this->assign('waitSecond','1');
			$this->success('导入成功');
		}else{
		    $this->error("上传失败！");
		}

    }





    private function getsubject(){
        $sid = $_SESSION['school_id'];
        $m = M('subject');
        $data['school_id'] = $sid;
        $list = $m->where($data)->select();
        $str1 = '';
        $str1.='<tr class="ui-form-item"><td  class="ui-label">任教科目：</td><td>';
        foreach($list as $k=>$l){
            $str1.='<label for="" class="checklabel"><input class="ui-checkbox" name="subject_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[KCMC].'</label>';
        }
        $str1.='</td></tr>';


        $m1 = M('class');
        $map['school_id'] = $sid;
        // $map['teacher_id'] = array('exp','is null');
        $list1 = $m1->where($map)->select();

        $str2.='';
        $str2.= '<tr class="ui-form-item"><td  class="ui-label">管理班级：</td><td>';
        foreach($list1 as $k=>$l){
            $str2.='<label for="" class="checklabel"><input class="ui-checkbox" name="class_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
        }
        $str2.='</td></tr>';

        $map1['school_id'] = $sid;
        $list2 = $m1->where($map1)->select();
        $str3.= '';
        $str3.= '<tr class="ui-form-item"><td  class="ui-label">执教班级：</td><td>';
        foreach($list2 as $k=>$l){
            $str3.='<label for="" class="checklabel"><input class="ui-checkbox" name="power_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
        }
        $str3.='</td></tr>';
        return  $str1.$str2.$str3;
    }



    private function addteacherinfo($schoolid,$map1,$map2) {
        $user = M('user');
        $userlist = $user->field('user.*')->where('SJHM="'.$map1[SJHM].'"')->join('teacher on teacher.user_id=user.id')->find();
        if (!empty($userlist))
        {
            $this->error('已存在手机号码为'.$map1['SJHM'].'的教师');
        }
        $userid = 0;
        if($userlist == null){
            $time = time();
            //$map1['addtime'] = $time;
            $map1['endtime'] = $time+(3600*24*365);
            //$map1['shenhe']=0;
            $map1['status']=0;
            $userid = $user->add($map1);
        }
        else{
            $data['id'] = $userlist['id'];
            $data['is_teacher'] = 1;
            $user->save($data);
            $userid = $userlist['id'];
        }



        $tea = M('teacher');
        if($userlist == null){
            $teachid = 0;
            $map2['user_id'] = $userid;
            if($teachid = $tea->add($map2)){
                return $teachid;
            }
        }else{
            $map3['user_id'] = $userid;
            $tealist = $tea->where($map3)->find();
            return $tealist['id'];
        }
    }


    private function school_subject(){
        $school_id = $_SESSION['school_id'];
        $m = M('subject');
        $data['school_id'] = $school_id;
        $list = $m->where($data)->select();
        return $list;
    }



    private function updateclass($ids,$teacherid){
        $set['teacher_id'] = $teacherid;
        $data['id'] = array('in',$ids);
        $m = M('class');
        $isok = $m->where($data)->save($set);
        if($isok){
//          add_log($_SESSION['adminid'],'修改执教班级',2);
        }
    }

    private function updateteacherpower($classid,$teacherid){
        $m = M('teacher_power');
        foreach($classid as $k=>$l){
            $data['teacher_id'] = $teacherid;
            $data['class_id'] =$l;
            $m->add($data);
        }
//      add_log($_SESSION['adminid'],'修改管理班级',2);
    }



    private function _editsubject($schoolid,$classid){
        $m = M('subject');
        $data['school_id'] = $schoolid;
        $list = $m->where($data)->select();
        $str1 = '';
        foreach($list as $k=>$l){
            if(in_array($l['id'],$classid)){
                $str1.='<label for="" class="checklabel"><input class="ui-checkbox" name="subject_id[]" id="test" value="'.$l[id].'" type="checkbox" checked>'.$l[KCMC].'</label>';
            }else{
                $str1.='<label for="" class="checklabel"><input class="ui-checkbox" name="subject_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[KCMC].'</label>';
            }
        }
        return $str1;
    }

    private function _editclass($schoolid,$tid){
        $m1 = M('class');
        $map['school_id'] = $schoolid;
        // $map['teacher_id'] = array(array('exp','is null'),array('eq',$tid),'or');
        $list1 = $m1->where($map)->select();
        foreach($list1 as $k=>$l){
            if($l['teacher_id'] == $tid){
                $str2.='<label for="" class="checklabel"><input class="ui-checkbox" name="class_id[]" id="test" value="'.$l[id].'" type="checkbox" checked>'.$l[class_name].'</label>';
            }else{
                $str2.='<label for="" class="checklabel"><input class="ui-checkbox" name="class_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
            }
        }
        return $str2;
    }

    private function _editpower($schoolid,$tid){
        $m = M('teacher_power');
        $data['school_id'] = $schoolid;
        $data['teacher_id'] = $tid;
        $list2 = $m->where($data)->select();
        foreach($list2 as $key=>$l){
            $c[$key] = $l['class_id'];
        }

        $class = M('class');
        $map['school_id'] = $schoolid;
        $list = $class->where($map)->select();
        foreach($list as $k=>$l){
            if(in_array($l['id'],$c)){
                $str3.='<label for="" class="checklabel"><input class="ui-checkbox" name="power_id[]" id="test" value="'.$l[id].'" type="checkbox" checked>'.$l[class_name].'</label>';
            }else{
                $str3.='<label for="" class="checklabel"><input class="ui-checkbox" name="power_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
            }
        }
        return $str3;
    }



    //删除操作

    function proc_del()
    {

        $id = $this->_loadids();
        $map1['id'] =array('in',$id);
        $mschool_proclamation=M('school_proclamation');
        $mschool_proclamation->where($map1)->delete();
        add_log($_SESSION['schooler_id'],'删除 ID:'.$id.'学校公告',2);
        $this->success('删除成功');

    }
    function parent_del()
    {
        $id = $this->_loadids();
<<<<<<< HEAD
        $map1['id'] =array('in',$id);
        $mstudent=M('student');
        $data['parent_id'] = '';
        $data['relation'] ='';
        add_log($_SESSION['schooler_id'],'删除 ID:'.$data['parent_id'].'家长关系',2);
=======
        $map1['parent_id'] =array('in',$id);
        $mppower=M('parent_power');
        $plist =$mppower->where($map1)->select();
        foreach ($plist as $pl) {
            if (getinfo($pl['student_id'],'student','school_id') == $_SESSION['school_id'])
            {
                $mppower->where('student_id='.$pl['student_id'].' and parent_id='.$pl['parent_id'])->delete();
                add_log($_SESSION['teacher_id'],'删除 Id:'.$pl['parent_id'].'家长关系',2);                
            }
            # code...
        }
>>>>>>> 4d10520e2d379bccf159d357fbc2e5519f23428b

        //add_log($_SESSION['teacher_id'],'删除 家长关系',2);
        //$mppower->where($map1)->delete();
        
        $this->success('删除成功');
    }
    function subject_del()
    {
        $id = $this->_loadids();
        $map1['id'] =array('in',$id);
        $map['subject_id'] =array('in',$id);
        $mlesson =M('lesson');
        $mresult =M('student_result');
        $count1 = $mlesson->where($map)->count();
        $count2 = $mresult->where($map)->count();
        if ($count1 >0 or $count2>0)
        {
            $this->error('科目使用中，不能删除');
        }
        if ($count1==0 and $count2==0 and $count3==0)
        {
            $msubject=M('subject');
            $msubject->where($map1)->delete();

            add_log($_SESSION['schooler_id'],'删除 ID:'.$id.'科目',2);
            $this->success('删除成功');
        }

    }
    function stu_del()
    {

        $id = $this->_loadids();
        $mremark=M('student_remark');
        $mattence =M('student_attence');
        $mresult=M('student_result');

        $map['student_id'] = array('in',$id);
        $map1['id'] =array('in',$id);


        $count1 = $mremark->where($map)->count();
        $count2 = $mattence->where($map)->count();
        $count3 = $mresult->where($map)->count();
        if ($count1>0)
        {
            $this->error('不可删除，学生有存在的点评记录');
        }
        if ($count2>0)
        {
            $this->error('不可删除，学生有存在的考勤记录');
        }
        if ($count3>0)
        {
            $this->error('不可删除，学生有存在的成绩记录');
        }
        if ($count1==0 and $count2==0 and $count3==0)
        {
            $mstudent=M('student');
            add_log($_SESSION['schooler_id'],'删除 ID:'.$id.'学生',2);

            $mstudent->where($map1)->delete();
            $this->success('删除成功');
        }


    }

	public function loadteabylesson()
    {
        $lesson_id =$_REQUEST['lesson_id'];
        $class_id = $_REQUEST['class_id'];
        $mtp = M('teacher_power');
        $plist = $mtp->where('class_id ='.$class_id)->select();
        foreach ($plist as $plt) {
            $tplist .= $plt['teacher_id'].',';
                        # code...
        }
        $tplist = rtrim ($tplist,',');
       // $subject_id = getinfo($lesson_id,'lesson','subject_id');
        if (!empty($lesson_id))
        {
            $mteacher=M('teacher');
            $teacherlist = $mteacher->where($lesson_id.' in (subject_id) and school_id='.$_SESSION['school_id'].' and id in ('.$tplist.')')->order('CONVERT(XM USING gbk)')->select();
            echo json_encode($teacherlist);
        }
    }


	function user_info(){
		$teacher_id = $_SESSION['schooler_id'];
		$school_id = $_SESSION['school_id'];


		$teacher = M('teacher');
		$tlist = $teacher->find($teacher_id);
		$this->assign('tlist',$tlist);


		$user = M('user');
		$ulist =  $user->find($tlist['user_id']);
		$this->assign('ulist',$ulist);

		$class1 = loadpower(2,1);
		$classid = explode(',',$class1);
		$map1['id'] = array('in',$classid);
		$class = M('class');
		$class1list = $class->where($map1)->select();
		$str1="";
		foreach($class1list as $k=>$l){
			$str1.=$l['class_name'].',&nbsp;&nbsp;';
		}
		$this->assign('c1list',$str1);


		$class2 = loadpower(1,1);
		$classid = explode(',',$class2);
		$map2['id'] = array('in',$class2);

		$class2list = $class->where($map2)->select();
		$str2="";
		foreach($class2list as $k=>$l){
			$str2.=$l['class_name'].',&nbsp;&nbsp;';
		}
		$this->assign('c2list',$str2);


		$school = M('school');
		$slist = $school->find($school_id);
		$this->assign('slist',$slist);


		$this->display();
	}


	function user_doinfo(){
		$school_id = $this->_isempty($_SESSION['school_id'],'学校ID不能为空');
		$teacher_id = $this->_isempty($_SESSION['schooler_id'],'教师ID不能为空');
		$user_id = $this->_isempty($_POST['userid'],'用户ID不能为空');

		$user = M('user');
		$teacher = M('teacher');
		$school = M('school');

		if($_POST['password'] != null && $_POST['password']!= ""){
			$userlist = $user->find($user_id);
			if($_POST['password'] != decrypt($userlist['password_des'])){
				$this->error('原密码错误');
				exit;
			}
		}

		if($_POST['newpassword1'] != null && $_POST['newpassword1'] != "" && $_POST['newpassword2'] != null && $_POST['newpassword2'] != ""){
			if($_POST['newpassword1'] == $_POST['newpassword2']){
				$data['id'] = $user_id;
				$data['password'] = $_POST['newpassword1'];
				$data['password_des'] = encrypt($_POST['newpassword1']);
				$user->save($data);
			}else{
				$this->error('两个新密码不相同');
			}
		}


		$map1['id'] = $_SESSION['schooler_id'];
		$map1['LXDH'] = $_POST['LXDH'];
		$map1['LXDZ'] = $_POST['LXDZ'];
		$map1['DZXX'] = $_POST['DZXX'];
		$map1['teacher_no'] = $_POST['teacher_no'];
		$map1['QQ'] = $_POST['QQ'];
		$map1['intro'] = $_POST['intro'];
		if($_FILES['pic']['name'] != null){
			$upload = $this->uploadimg('teacher/');
			$pic = substr($upload[0]['savepath'].$upload[0]['savename'],2);
			$map1['pic'] = $pic;
		}
		$teacher->save($map1);


		$map2['id'] = $_POST['userid'];
		$map2['SJHM'] = $_POST['SJHM'];
		$user->save($map2);

		$this->success("修改成功");

	}























}